package com.ihr360.payroll.controller.spreadsheet.validator;

import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.excel.specification.ColumnSpecification;
import com.ihr360.excel.specification.CommonSpecification;
import com.ihr360.excel.validation.SpreadSheetValidator;
import com.ihr360.excel.validation.annonation.SpreadSheetValid;
import com.ihr360.excel.vo.CustomSpreadSheetValidateResult;
import com.ihr360.excel.vo.SpreadSheetRow;
import com.ihr360.excel.vo.SpreadSheetSpecification;
import com.ihr360.excel.vo.SpreadSheetValidatorError;
import com.ihr360.excel.vo.SpreadSheetValidatorRequest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@SpreadSheetValid(code = "test")
public class DemoSpreadSheetValidator implements SpreadSheetValidator {


    @Override
    public CustomSpreadSheetValidateResult customValidate(SpreadSheetValidatorRequest request) {

        CustomSpreadSheetValidateResult result = new CustomSpreadSheetValidateResult();
        List<SpreadSheetRow> spreadSheetRows = request.getSpreadSheetDatas();
        if (CollectionUtils.isEmpty(spreadSheetRows)) {
            return new CustomSpreadSheetValidateResult();
        }
        List<SpreadSheetValidatorError> errors = Lists.newArrayList();

        SpreadSheetValidatorError spreadSheetValidatorError = new SpreadSheetValidatorError();
        spreadSheetValidatorError.setRowNum(2);
        spreadSheetValidatorError.setColNum(1);
        spreadSheetValidatorError.setMsg("这是一行自定义校验测试报错数据。");
        errors.add(spreadSheetValidatorError);

        result.setErrors(errors);

//        CorrectSpreadsheetRowEntity entity = new CorrectSpreadsheetRowEntity();
//        entity.setHasPermission(true);
//        entity.setMobileNo("134123232");
//        entity.setStaffName("测试convert数据");
//        List<CorrectSpreadsheetRowEntity> convertDatas = Lists.newArrayList(entity);
//
//        SpreadSheetValidInterruptError interruptError = new SpreadSheetValidInterruptError();
//        interruptError.setDefaultLogMsg("自定义中断性错误！");

//        result.setInterruptError(interruptError);

//        try {
//            result.setConvertResult(SpreadSheetConvertResultUtil.ObjectToByte(convertDatas));
//        } catch (IOException e) {
//            throw new Ihr360Exception(null, "获取自定义校验规则失败！");
//        }

        return result;
    }

    @Override
    public SpreadSheetSpecification commonSpecification(SpreadSheetValidatorRequest request) {
        SpreadSheetSpecification spreadSheetSpecification = new SpreadSheetSpecification();
        List<ColumnSpecification> columnSpecifications = new ArrayList<>();
        spreadSheetSpecification.setColumnSpecifications(columnSpecifications);

        ColumnSpecification nullBaleDateColumnSpe = ColumnSpecification.builder()
                .cellType(Date.class)
                .allowNull(true)
                .columns(Lists.newArrayList("日期列"))
                .build();
        ColumnSpecification notNullColumnSpe = ColumnSpecification.builder()
                .cellType(String.class)
                .allowNull(false)
                .columns(Lists.newArrayList("公司名称"))
                .build();

        columnSpecifications.add(nullBaleDateColumnSpe);
        columnSpecifications.add(notNullColumnSpe);


        CommonSpecification commonSpecification = CommonSpecification.builder()
                .staffNameAlias(Lists.newArrayList("姓名"))
                .mobileNoAlias(Lists.newArrayList("手机号码", "手机号"))
                .build();


        spreadSheetSpecification.setCommonSpecification(commonSpecification);
        return spreadSheetSpecification;
    }

}
